package com.wangyk.rabbitmqconsole.controller;

import com.wangyk.rabbitmqcommon.model.console.User;
import com.wangyk.rabbitmqconsole.service.UserService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description:
 * @Author: wangyk
 * @CreateDate: 2018/11/20$ 17:55$
 * @UpdateUser: wangyk
 * @UpdateDate: 2018/11/20$ 17:55$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Controller
public class RabbitMqController
{
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Resource
    private UserService userService;

    /**
     * 一对一发送消息
     * http://localhost:8080/send
     */
    @RequestMapping("/send")
    @ResponseBody
    public String send()
    {
        String content = "Date:" + new Date();
        /**
         *  amqpTemplate.convertAndSend("wangykTest1", content);
         *  参数：wangykTest1队列名，content内容
         * */
        amqpTemplate.convertAndSend("wangykTest1", content);
        return content;
    }

    /**
     * 一对多发送消息
     * http://localhost:8092/rabbitmq/multiSend.do
     */
    @RequestMapping("/multiSend.do")
    @ResponseBody
    public String multiSend()
    {
        StringBuilder times = new StringBuilder();
        User user=userService.selectUserById(1);
        for (int i = 0; i < 10; i++)
        {
            long time = System.nanoTime();
            amqpTemplate.convertAndSend("wangykTest2", "第" + i + "次发送的时间：" + time);
            times.append(time + "<br>");
        }
        return times.toString();
    }

    /**
     * 多对多发送消息
     * http://localhost:8080/multi2MultiSend
     */
    @RequestMapping("/multi2MultiSend")
    @ResponseBody
    public String mutil2MutilSend()
    {
        StringBuilder times = new StringBuilder();
        for (int i = 0; i < 10; i++)
        {
            long time = System.nanoTime();
            amqpTemplate.convertAndSend("lyhTest1", "第" + i + "次发送的时间：" + time);
            amqpTemplate.convertAndSend("lyhTest2", "第" + i + "次发送的时间：" + time);
            times.append(time + "<br>");
        }
        return times.toString();
    }

    /**
     * 多对多发送消息
     * http://localhost:8080/topicSend1
     */
    @RequestMapping("/topicSend1")
    @ResponseBody
    public String topicSend1()
    {
        String context = "my topic 1";
        System.out.println("发送者说 : " + context);
        this.amqpTemplate.convertAndSend("exchange", "topic.message", context);
        return context;
    }

    /**
     * 多对多发送消息
     * http://localhost:8080/topicSend2
     */
    @RequestMapping("/topicSend2")
    @ResponseBody
    public String topicSend2()
    {
        String context = "my topic 2";
        System.out.println("发送者说 : " + context);
        this.amqpTemplate.convertAndSend("exchange", "topic.messages", context);
        return context;
    }
}
